package com.guangjian.asc.retail.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

import com.guangjian.common.sql.CustomerSQL;

@RooJavaBean
@RooToString
@RooEntity
public class SalesOrder {

	@NotNull
	@Column(unique = true)
	private String customerName;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	private Date saleTime;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	private OrderTemplate orderTemplate;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "salesOrder")
	private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();

	public List<OrderDetail> findOrderDetails() {

		TypedQuery<OrderDetail> q = entityManager().createQuery(
				CustomerSQL.getSql("retail_sql.xml", "search.OrderDetail.by.SalesOrder"), OrderDetail.class);

		q.setParameter("salesOrder", this);

		return q.getResultList();
	}
}
